/***************************************************************************
 *   Copyright (C) 2004 by cyril dupuit                                    *
 *   cyrildupuit@hotmail.com                                               *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

//*****************************************************************************
// Nom du module : MsgQInfo.c
// Description :  Obtention d'infos sur les files de messages.
//*****************************************************************************
 
// Historique :
// 28/10/04 : Creation du module V1.0  

#include "Kernel/Core.h"
#include "Misc/Console.h"
#include "Lib/Queue.h"
#include "Misc/MsgQInfo.h"
#include "Kernel/Schedule.h"
#include "Arch/Pentium/Kernel/Cpu.h"
#include "Kernel/Parental.h"
#include "Arch/Pentium/Kernel/malloc.h"
#include "Lib/StdLib/stdio.h"
#include "Lib/StdLib/string.h"
#include "Kernel/Thread.h"
#include "Kernel/Errors.h"
#include "Kernel/sMsgQ.h"

//*****************************************************************************
// But de la fonction : Afficher une liste de toute les files de messages crees
// Entree : Aucune
// Parametre retourne : TRUE si affichage d'une liste, FALSE si liste est vide,
// ERROR si memoire insuffisante
//*****************************************************************************
Status_t SmqListShow(void)
{
	QueueID_t QueueSmqList;
	SmqID_t SmqID;
	Count_t QueueUsed, QueueSize;
	unsigned int Line = 0;
	Coord_t X, Y;
	unsigned int SMsgQNbr;
	
	SMsgQNbr = SmqListCount();
	if(SMsgQNbr == 0)
	{
		if(printf("Aucune file de messages cree.\n") == -1) return ERROR;
		return FALSE;
	}
	QueueSmqList = QueueCreate(SMsgQNbr, QUEUE_FIFO);
	if(QueueSmqList == 0)
	{//Creation de la file impossible
		SetLastError(INSUFFICIENT_MEMORY_AVAILABLE);
		return ERROR;
	}
	else
	{//Creation reussie	
		if(printf("Liste des files de messages declarees (%d) : \n", SMsgQNbr) == -1) return ERROR;
		if(SmqGetList(QueueSmqList) == FALSE)
		{//Un semaphore a ete cree entre temps, la file d'attente est trop petite
			if(printf("La liste ci-dessous est incomplete, une file de message vient d'etre cree !\n") == -1) return ERROR;	
		}
		if(ConsoleGetCursorPosition(&X, &Y) == ERROR) return ERROR;
		if(printf("ID : ") == -1) return ERROR;
		GotoXY(X + 20, Y);
		if(printf("Taille file d'attente : \n") == -1) return ERROR;
		//Affichage des parametres des taches
		SmqID = (SmqID_t)QueueRead(QueueSmqList);
		while(SmqID != 0)
		{//Afficher la liste des taches declarees
			if(ConsoleGetCursorPosition(&X, &Y) == ERROR) return ERROR;
			GotoXY(X , Y);
			if(printf("%ld", SmqID) == -1) return ERROR;
			//Afficher le nombre de place utilise dans la file d'attente
			GotoXY(X + 20, Y);
			if(SmqSize(SmqID, &QueueUsed, &QueueSize) == ERROR) return ERROR;
			if(printf("%d/%d\n", QueueUsed, QueueSize) == -1) return ERROR;
			//Recuperer le prochain numero du semaphore
			SmqID = (SmqID_t)QueueRead(QueueSmqList);
			Line = (Line + 1) % 20;//20 equivaut au nombre de ligne sur la console
			if((Line == 0)&&(SmqID != 0))
			{//Demander a l'utilisateur de presser une touche pour voir la suite de l'ecran
				if(Pause() != TRUE)
				{//L'utilisateur stoppe la visualisation
					QueueDelete(&QueueSmqList);
					
					return TRUE;
				}
			}
		}
	}
	QueueDelete(&QueueSmqList);
	
	return TRUE;
}

//*****************************************************************************
